package migration

import (
	"center/db/models"
	"context"
	"gorm.io/gorm"
)

type Migration struct {
	db *gorm.DB
}

func NewMigration(db *gorm.DB) *Migration {
	return &Migration{
		db: db,
	}
}

func (receiver *Migration) Run(ctx context.Context) {
	receiver.UpdateTables(ctx)
}

func (receiver *Migration) UpdateTables(ctx context.Context) error {
	err := receiver.db.WithContext(ctx).AutoMigrate(
		&models.Bucket{},
		&models.Segment{},
		&models.Object{},
		&models.Node{},
		&models.User{},
		&models.Repair{},
		&models.ObjectInline{},
	)
	if err == nil {
		receiver.Mock(ctx)
	}
	return err
}

func (receiver *Migration) Mock(ctx context.Context) error {
	receiver.mockUserBucket()
	//receiver.mockNodes(20)
	return nil
}
